﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using SPMS.Utility;
using SPMS.Log.Core;

namespace SPMS.Log.Formatter
{
    /// <summary>
    /// 抽象日志格式化器
    /// </summary>
    /// <remarks>提供了日志格式化器的默认实现基类</remarks>
    /// <history>
    /// [zanezeng]               2009/9/29 14:10    创建
    /// </history>
    public abstract class AbstractFormatter : IFormatter
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="AbstractFormatter"/>的新实例
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/29 14:12    创建
        /// </history>
        protected AbstractFormatter()
        {
        }

        #endregion

        #region ---IFormatter

        /// <summary>
        /// 获得或者设置日志记录的页头
        /// </summary>
        /// <value>日志记录的页头</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/28 11:17    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/29 14:12    创建
        /// </history>
        public string Header
        {
            get
            {
                return this.m_Header;
            }
            set
            {
                this.m_Header = value;
            }
        }

        /// <summary>
        /// 获得或者设置日志记录的页脚
        /// </summary>
        /// <value>日志记录的页脚</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/28 11:19    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/29 14:13    创建
        /// </history>
        public string Footer
        {
            get
            {
                return this.m_Footer;
            }
            set
            {
                this.m_Footer = value;
            }
        }

        /// <summary>
        /// 将给定的日志条目按照一个有序的字符串序列记录到给定的字符串写入器中
        /// </summary>
        /// <param name="writer">用于记录日志信息的字符串写入器</param>
        /// <param name="entry">要进行格式化的日志记录</param>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="writer"/>为<c>null</c>引用</exception>
        /// <exception cref="ArgumentNullException"><paramref name="entry"/>为<c>null</c>引用</exception>
        /// <history>
        /// [zanezeng]               2009/9/28 11:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 10:10    创建
        /// </history>
        public void Format( TextWriter writer, LogEntry entry )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( writer, "writer" );
            ArgumentAssert.AssertArgumentNotNull( entry, "entry" );

            //格式化日志信息
            string message = this.FormatEntry( entry );

            //写入日志条目
            writer.WriteLine( message );
        }

        /// <summary>
        /// 将给定的日志条目格式化为一个有序的字符串序列
        /// </summary>
        /// <param name="entry">要进行格式化的日志记录</param>
        /// <returns>描述给定日志记录的一个有序的字符串序列</returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="entry"/>为<c>null</c>引用</exception>
        /// <history>
        /// [zanezeng]               2010/1/13 10:03    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 10:10    创建
        /// </history>
        public string Format( LogEntry entry )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( entry, "entry" );

            //格式化日志条目
            return this.FormatEntry( entry );
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得或者设置格式化提供者对象
        /// </summary>
        /// <value>格式化提供者对象</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/30 12:27    创建
        /// </history>
        public IFormatProvider FormatProvider
        {
            get
            {
                //判断是否设置了格式化提供对象
                if (null == this.m_FormatProvider)
                {
                    //如果没有设置格式化提供对象，则返回与区域性无关的格式化提供者
                    return System.Globalization.CultureInfo.InvariantCulture;
                }

                //返回设置的格式化提供者
                return this.m_FormatProvider;
            }
            set
            {
                this.m_FormatProvider = value;
            }
        }

        /// <summary>
        /// 设置用于格式化日志信息的区域名称
        /// </summary>
        /// <value>用于格式化日志信息的区域名称</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 10:02    创建
        /// </history>
        public string CultureName
        {
            set
            {
                //设置格式化提供者对象
                this.FormatProvider = CultureInfo.GetCultureInfo( value );
            }
        }

        #endregion

        #region ---Protected Method

        /// <summary>
        /// 将给定的对象格式化为字符串
        /// </summary>
        /// <param name="obj">要格式化的对象</param>
        /// <returns>给定对象的字符串描述</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/12 14:19    创建
        /// </history>
        protected string FormatObject( object obj )
        {
            //调用对象格式化器格式化
            return LoggingContext.ObjectFormatterRegistry.FormatObject( obj, null, this.FormatProvider );
        }

        /// <summary>
        /// 将给定的日志条目格式化为一个有序的字符串序列
        /// </summary>
        /// <param name="entry">要进行格式化的日志记录</param>
        /// <returns>描述给定日志记录的一个有序的字符串序列</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 10:19    创建
        /// </history>
        protected abstract string FormatEntry( LogEntry entry );

        #endregion

        #region ---Field

        /// <summary>
        /// 日志记录的页头
        /// </summary>
        private string m_Header;

        /// <summary>
        /// 日志记录的页脚
        /// </summary>
        private string m_Footer;

        /// <summary>
        /// 格式化提供者对象
        /// </summary>
        private IFormatProvider m_FormatProvider;

        #endregion
    }
}
